package com.winHandle.load;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import com.common.ConfigParam;
import com.common.FrameUtil;
import com.common.MessageShow;
import com.db.TestDB;

public class LoadWin extends JFrame implements ActionListener{

	/**
	 *
	 */
	private static final long serialVersionUID = -6814889240589670028L;

	/* 窗口框架 */
	private JFrame jframe ;

	/* 数据库ip */
	private JLabel dbIP ;
	private JTextField dbIP_v ;

	/* 数据库名 */
	private JLabel dbName ;
	private JTextField dbName_v ;

	/* 库登陆名 */
	private JLabel name ;
	private JTextField name_v ;

	/* 库密码 */
	private JLabel passW = new JLabel("密码");
	private JTextField passW_v = new JTextField();

	/* 提交按钮 */
	private JButton commitBtn;

	public void excute(){

		jframe = this;
		int inc = 40;// 增量
		int hight = 60;// 高度坐标

		List<Object> component = new ArrayList<Object>();

		jframe.setSize(300,350);
		Toolkit kit = Toolkit.getDefaultToolkit();
		jframe.setLocation((kit.getScreenSize().width-300)/2, (kit.getScreenSize().height-350)/2);
		jframe.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

		jframe.setLayout(null);

		dbIP = new JLabel("数据库IP");
		dbIP_v = new JTextField();

		dbIP.setBounds(30, hight, 67, 18);
		dbIP_v.setBounds(100,hight,130,18);

		component.add(dbIP);
		component.add(dbIP_v);
		hight += inc;

		dbName = new JLabel("数据库名");
		dbName_v = new JTextField();

		dbName.setBounds(30, hight, 67, 18);
		dbName_v.setBounds(100,hight,130,18);

		component.add(dbName);
		component.add(dbName_v);
		hight += inc;

		name = new JLabel("用户名");
		name_v = new JTextField();

		name.setBounds(30, hight, 67, 18);
		name_v.setBounds(100,hight,130,18);

		component.add(name);
		component.add(name_v);
		hight += inc;

		passW = new JLabel("密码");
		passW_v = new JTextField();

		passW.setBounds(30, hight, 67, 18);
		passW_v.setBounds(100,hight,130,18);

		component.add(passW);
		component.add(passW_v);

		//  内容填充
		if(!"".equals(ConfigParam.get("IP"))){
			dbIP_v.setText(ConfigParam.get("IP"));
		}
		if(!"".equals(ConfigParam.get("DBNAME"))){
			dbName_v.setText(ConfigParam.get("DBNAME"));
		}
		if(!"".equals(ConfigParam.get("NAME"))){
			name_v.setText(ConfigParam.get("NAME"));
		}
		if(!"".equals(ConfigParam.get("PASSW"))){
			passW_v.setText(ConfigParam.get("PASSW"));
		}

		hight += 60;

		commitBtn = new JButton("  提交    ");
		commitBtn.setBounds(80,hight,80,25);
		commitBtn.addActionListener(this);

		component.add(commitBtn);

		FrameUtil.addComponent(jframe, component);
		FrameUtil.addIcon(this);

		jframe.setVisible(true);
	}

	/**
	 * 监听事件
	 *
	 *
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource()==commitBtn){

			String ip = dbIP_v.getText();
			String dbNameStr = dbName_v.getText();
			String nameStr = name_v.getText();
			String passWStr = passW_v.getText();
			if("".equals(ip)){
				MessageShow.warn("数据库IP不能为空！");
				return ;
			}
			if("".equals(dbNameStr)){
				MessageShow.warn("数据库名不能为空！");
				return ;
			}
			if("".equals(nameStr)){
				MessageShow.warn("用户名不能为空！");
				return ;
			}
			if("".equals(passWStr)){
				MessageShow.warn("密码不能为空！");
				return ;
			}
			Map<String, String> params = new HashMap<String, String>();
			params.put("IP", ip);
			params.put("DBNAME", dbNameStr);
			params.put("NAME", nameStr);
			params.put("PASSW", passWStr);
			ConfigParam.refreshParam(params);
			String msg = TestDB.test();
			if(true && "OK".equals(msg)){

				jframe.dispose();
				new MainWin_T("数据库笔记工具");

			}else{
				MessageShow.warn( "连接信息错误！【"+msg+"】");
			}
		}
	}

	public LoadWin() {
		super();
		// TODO Auto-generated constructor stub
		excute();
	}

	public LoadWin(String title){
		super(title);
		excute();
	}
}
